package com.itheima.itheima_health.service.Impl;

import com.itheima.itheima_health.constant.MessageConstant;
import com.itheima.itheima_health.entity.Result;
import com.itheima.itheima_health.exception.BaseException;
import com.itheima.itheima_health.mapper.OrdersettingMapper;
import com.itheima.itheima_health.pojo.OrderSetting;
import com.itheima.itheima_health.service.OrdersettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.*;

@Service
public class OrdersettingServiceImpl implements OrdersettingService {
    @Autowired
    private OrdersettingMapper ordersettingMapper;
    @Override
    public void add(List<OrderSetting> orderSettings) {
        if (orderSettings!=null && orderSettings.size()>0){
            //批量导入预约设置
            for (OrderSetting orderSetting : orderSettings) {
                //该日期有没有导数据
                Long count = ordersettingMapper.findCountByOrderDate(orderSetting);
                if (count>0){
                    ordersettingMapper.update(orderSetting);
                }else{
                    ordersettingMapper.add(orderSetting);
                }
            }
        }
    }

    @Override
    public List<Map> getOrderSettingByMonth(String data) {
        String[] parts = data.split("-");

        /*String begin = data+"-1";
        String end = data+"-31";*/

        // 获取年份和月份
        String year = parts[0];
        String month = parts[1];

        // 构建当月的第一天
        String begin = year + "-" + month + "-01"; // 当月第一天
        // 根据月份和闰年判断，构建当月的最后一天
        String end = year + "-" + month + "-" + getLastDayOfMonth(Integer.parseInt(year), Integer.parseInt(month));

        /*DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate date = LocalDate.parse(dateStr, formatter);
        System.out.println(date); // 输出: 2024-03-26*/

        Map<String,String> map = new HashMap<>();
        map.put("begin",begin);
        map.put("end",end);
        List<OrderSetting> orderSettings = ordersettingMapper.getOrderSettingByMonth(map);
        List<Map> result = new ArrayList<>();

        if (orderSettings!=null&&orderSettings.size()>0){
            for (OrderSetting orderSetting : orderSettings) {
                Map<String, Object> m = new HashMap<>();
                m.put("date",orderSetting.getOrderDate().getDate());
                m.put("number",orderSetting.getNumber());
                m.put("reservations",orderSetting.getReservations());
                result.add(m);
            }
        }
        return result;
    }
    // 获取指定年份和月份的最后一天
    private String getLastDayOfMonth(int year, int month) {
        // 大月（31天）：1, 3, 5, 7, 8, 10, 12
        if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
            return "31";
        }
        // 小月（30天）：4, 6, 9, 11
        else if (month == 4 || month == 6 || month == 9 || month == 11) {
            return "30";
        }
        // 2月
        else if (month == 2) {
            // 闰年判断
            if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
                return "29"; // 闰年2月29天
            } else {
                return "28"; // 平年2月28天
            }
        } else {
            throw new IllegalArgumentException("Invalid month: " + month);
        }
    }
    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        try {
            long count = ordersettingMapper.findCountByOrderDate(orderSetting);
            if(count > 0){
                //当前日期已经进行了预约设置，需要进行修改操作
                ordersettingMapper.editNumberByOrderDate(orderSetting);
            }else{
                //当前日期没有进行预约设置，进行添加操作
                ordersettingMapper.add(orderSetting);
            }
        }catch (Exception e){
            throw  new BaseException(MessageConstant.ORDERSETTING_FAIL);
        }
    }
}
